package com.newmedia.gui;

import java.awt.Graphics;

import com.newmedia.main.Main;

//Class representing the key that the user chooses.
//Finds the correct notes that belong in the key, and populates an array of them for other classes to use.
//Handles printing of note labels on the Y axis.
public class Key {
	
	private Main.mKey m_Root;
	private String[] m_KeyNotes;
	
	private int index = 0;
	private int sharps = 0;
	private int flats = 0;
	
	int m_Scale = 3 * GraphicMusic.scale; 
	
	Graphics m_Graphics;
	
	public Key(Main.mKey root, Graphics g) {
		m_Graphics = g;
		m_Root = root;
		m_KeyNotes = new String[8];
	}
	
	public void indexKey() {
		//Match our current key choice to an iteratable array of the musical alphabet, also is the ugliest switch statement ever.
		switch(m_Root){
		case A: 
			index = 0;
			sharps = 3;
			flats = 0;
			break;
		case Bb:
			index = 1;
			sharps = 0;
			flats = 2;
			break;
		case B:
			index = 1;
			sharps = 5;
			flats = 0;
			break;
		case C:
			index = 2;
			sharps = 0;
			flats = 0;
			break;
		case Db:
			index = 3;
			sharps = 0;
			flats = 5;
			break;
		case D:
			index = 3;
			sharps = 2;
			flats = 0;
			break;
		case Eb:
			index = 4;
			sharps = 0;
			flats = 3;
			break;
		case E:
			index = 4;
			sharps = 4;
			flats = 0;
			break;
		case F:
			index = 5;
			sharps = 0;
			flats = 1;
			break;
		case Gb:
			index = 6;
			sharps = 0;
			flats = 5;
			break;
		case G:
			index = 6;
			sharps = 1;
			flats = 0;
			break;
		}
	}
	
	public void drawLabels(){
		
		//Set the scale factor.
		m_Scale = 3 * GraphicMusic.scale;
		
		//Handling for sharp keys. These next two functions take advantage of the fact that sharps are always added 
		//in the order FCGDAEB and flats are always added in the order BEADGCF.
		if(sharps != 0){
			for(int j = 0; j < 8; j++){
				
				if(sharps > 0 && Main.mAlphabet[index] == 'F'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";
				}
				else if(sharps > 1 && Main.mAlphabet[index] == 'C'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else if(sharps > 2 && Main.mAlphabet[index] == 'G'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else if(sharps > 3 && Main.mAlphabet[index] == 'D'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else if(sharps > 4 && Main.mAlphabet[index] == 'A'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else if(sharps > 5 && Main.mAlphabet[index] == 'E'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else if(sharps > 6 && Main.mAlphabet[index] == 'B'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "#", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "#";

				}
				else{
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]), 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]);

				}
				
				if(Main.mAlphabet[index] == 'G')
					index = 0;
				else
					index++;
				m_Scale+=GraphicMusic.scale;
			}
		}
		
		//Handling for flat keys. 
		//TODO implement this correctly. We only really handle major, natural keys at the moment.
		else{
			for(int j = 0; j < Main.mAlphabet.length; j++){
				
				if(flats > 0 && Main.mAlphabet[index] == 'B'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 1 && Main.mAlphabet[index] == 'E'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 2 && Main.mAlphabet[index] == 'A'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 3 && Main.mAlphabet[index] == 'D'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 4 && Main.mAlphabet[index] == 'G'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 5 && Main.mAlphabet[index] == 'C'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else if(flats > 6 && Main.mAlphabet[index] == 'F'){
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]) + "b", 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]) + "b";
				}
				else{
					m_Graphics.drawString(Character.toString(Main.mAlphabet[index]), 5, GraphicMusic.sizeHeight - m_Scale);
					m_KeyNotes[j] = Character.toString(Main.mAlphabet[index]);
				}
				
				//Reset to the beginning of the alphabet if we reach the end before we're done looping.
				if(Main.mAlphabet[index] == 'G')
					index = 0;
				else
					index++;
				m_Scale+=GraphicMusic.scale;
			}
		}
	}
	
	
	public String[] getKey(){
		return m_KeyNotes;
	}
}
